﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PROFIP.Utils
{
    class Utilities
    {
        /**
         * Parse Text into number
         */
        public static double parseNumber(String text)
        {
            if (text == null || text.Trim().Length == 0)
            {
                return 0;
            }
            return double.Parse(text);
        }

        /**
         * Receives a RadioButton or CheckBox and, if checked, activates
         * every control in the list, otherwise deactivates them.
         */
        public static void propagateCheck(Control control, List<Control> controls)
        {
            bool Checked = (control is RadioButton && ((RadioButton)control).Checked) ||
                (control is CheckBox && ((CheckBox)control).Checked);

            foreach (Control ctrl in controls)
            {
                ctrl.Enabled = Checked;
                if (!ctrl.Enabled)
                {
                    if (ctrl is RadioButton)
                    {
                        ((RadioButton)ctrl).Checked = false;
                    }
                    else if (ctrl is CheckBox)
                    {
                        ((CheckBox)ctrl).Checked = false;
                    }
                    else if (ctrl is RichTextBox)
                    {
                        ((RichTextBox)ctrl).Text = "";
                    }
                }
            }
        }

        /**
         * Makes a ListView act like only 1 checkbox may be selected
         */
        public static void SingleSelect_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (e.Item.Checked == true)
            {
                ListView listView = ((ListView)sender);
                foreach (ListViewItem item in listView.CheckedItems)
                {
                    if (item != e.Item)
                    {
                        item.Checked = false;
                    }
                }
            }
        }

        /**
         * Checks an item in the list
         */
        public static void checkIndice(ListView list, int index)
        {
            if (index < 0) return;
            list.Items[index].Checked = true;
        }

        /**
         * Gets the item index that is checked in the list
         */
        public static int getCheckedIndice(ListView list)
        {
            if (list.CheckedIndices != null &&
                list.CheckedIndices.Count > 0)
            {
                return list.CheckedIndices[0];
            }
            return -1;
        }


    }
}
